package com.h3c.ptability.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.ptability.entity.PtMsgTemplate;
import com.h3c.ptability.service.IPtMsgTemplateService;
import com.h3c.ptability.dto.PtMsgTemplateDTO;
import com.h3c.ptability.utils.ReturnCodeData;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * Copyright (C), 2022-2022, h3c
 * FileName: PtMsgTemplateController
 *
 * @Author: l22886
 * @Date: 2022-03-14
 * Description: pt_msg_template controller
 * Version: 1.0
 */
@Api(tags = " PtMsgTemplate 接口")
@CrossOrigin
@Slf4j
@RestController
@RequestMapping(value = "/ptMsgTemplate",produces = "application/json;charset=utf-8")
public class PtMsgTemplateController {

    @Resource
    private IPtMsgTemplateService ptMsgTemplateService;

    /**
     * @title: queryFuzzyListForPage
     * @description: 列表分页模糊查询
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtMsgTemplateDTO", paramType = "query"),
        @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryFuzzyListForPage", method = RequestMethod.GET)
    public Result queryFuzzyListForPage(@ModelAttribute PtMsgTemplateDTO paramsDTO,
                                                            @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                            @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //分页
        Page page = PageHelper.startPage(currentPage, pageSize);
        //执行
        List<PtMsgTemplateDTO> resultDTOList = ptMsgTemplateService.selectFuzzyByDto(paramsDTO);
        PageInfo<PtMsgTemplateDTO> pageInfo = page.toPageInfo();
        pageInfo.setList(resultDTOList);
        log.info("执行列表分页模糊查询完成，准备返回的结果为：" + JSON.toJSONString(pageInfo));
        return new Result(true, 20000, "列表分页模糊查询成功", pageInfo);
    }

    /**
     * 根据appid和模板编号获取单个邮件模板 多个取第一个
     * @param appid
     * @param code
     * @return
     */
    @RequestMapping(value = "/msgTemplate/{appid}/{code}", method = RequestMethod.GET)
    public Result<PtMsgTemplate> getMsgTemplate(@PathVariable("appid")String appid,@PathVariable("code")String code) {
        log.info("准备执行  根据appid和模板编号获取，传入的参数为：" + appid + "--" + code);
        PtMsgTemplate one = ptMsgTemplateService.getOne(new QueryWrapper<PtMsgTemplate>()
                .eq("app_id", appid)
                .eq("msg_template_code", code)
                .last("LIMIT 1"));

        log.info("查询完成，准备返回的结果为：" + JSON.toJSONString(one));
        return new Result<>(true, 20000, "列表分页模糊查询成功", one);
    }

    /**
     * 根据appid和模板类型获取邮件模板
     * @param appId
     * @param type
     * @return
     */
    @RequestMapping(value = "/getMsgTemplateByType", method = RequestMethod.GET)
    public Result<List<PtMsgTemplate>> getMsgTemplate(@RequestParam("appId")String appId,@RequestParam("type")Integer type) {
        log.info("准备执行  根据appid和模板类型获取，传入的参数为：" + appId + "--" + type);
        List<PtMsgTemplate> msgTemplates = ptMsgTemplateService.list(new QueryWrapper<PtMsgTemplate>()
                .eq("app_id", appId)
                .eq("msg_template_type", type));
        return new Result<>(true, 20000, "列表分页模糊查询成功", msgTemplates);
    }


    /**
     * @title: queryListForPage
     * @description: 列表分页精确查询
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtMsgTemplateDTO", paramType = "query"),
    @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
    @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryListForPage", method = RequestMethod.GET)
    public Result queryListForPage(@ModelAttribute PtMsgTemplateDTO paramsDTO,
                                                       @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                       @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //分页
        Page page = PageHelper.startPage(currentPage, pageSize);
        //执行
        List<PtMsgTemplateDTO> resultDTOList = ptMsgTemplateService.selectFuzzyByDto(paramsDTO);
        PageInfo<PtMsgTemplateDTO> pageInfo = page.toPageInfo();
        pageInfo.setList(resultDTOList);
        log.info("执行列表分页查询完成，准备返回的结果为：" + JSON.toJSONString(pageInfo));
        return new Result(true, 20000, "列表分页查询成功", pageInfo);
    }


    /**
     * @title: save
     * @description: 数据插入
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据插入", notes = "数据插入")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtMsgTemplate", paramType = "body")})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody PtMsgTemplateDTO paramsDTO) {
        log.info("准备执行 数据插入，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptMsgTemplateService.insertDto(paramsDTO);
        log.info("执行 数据插入完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据插入成功", result);
    }

    /**
     * @title: update
     * @description: 数据修改
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据修改", notes = "数据修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtMsgTemplate", paramType = "body")})
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody PtMsgTemplateDTO paramsDTO) {
        log.info("准备执行 数据修改，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptMsgTemplateService.updateDto(paramsDTO);
        log.info("执行 数据修改完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据修改成功", result);
    }

    /**
     * @title: delete
     * @description: 数据删除
     * @params: [ id ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据删除", notes = "数据删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtMsgTemplate", paramType = "body")})
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(@RequestBody PtMsgTemplateDTO paramsDTO) {
        log.info("准备执行 数据删除，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptMsgTemplateService.deleteDto(paramsDTO);
        log.info("执行 数据删除 完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据删除", result);
    }

    /**
     * 查询该appId下的所有邮件模板
     * @param appId
     * @param workerInfo
     * @return
     */
    @GetMapping("/getMsgTemplateOfAppId")
    public Result<List<PtMsgTemplate>> getMsgTemplateOfAppId(@RequestParam("appId") String appId,
                                                            @RequestParam(value = "workerInfo", required = false) String workerInfo){
        List<PtMsgTemplate> msgTemplateList;
        if (workerInfo == null){
            msgTemplateList = ptMsgTemplateService.list(new QueryWrapper<PtMsgTemplate>().lambda()
                    .eq(PtMsgTemplate::getAppId, appId));
        }else {
            msgTemplateList = ptMsgTemplateService.list(new QueryWrapper<PtMsgTemplate>().lambda()
                    .eq(PtMsgTemplate::getAppId, appId)
                    .in(StringUtils.hasText(workerInfo), PtMsgTemplate::getCreatorInfo, Arrays.asList(workerInfo.split(","))));
        }
        return new Result<>(true, 20000, "模板查询成功", msgTemplateList);
    }

}
